<?php
function em_use_ssl($info) {
	if( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) {
		return preg_replace('/http:/', 'https:', $info);
	}
	return $info;
}
add_filter('option_siteurl', 'em_use_ssl');

function em_add_menu_pages() {
	global $parent_file, $submenu_file;
	add_management_page("Events", "Events", 1, "EventManager/event-list.php");
	add_options_page("Event Management", "Event Management", 8, "EventManager/event-options.php");

	if (strpos($_GET['page'], "event-options.php") == false && strpos($_GET['page'], "Event") > -1) {
		$parent_file = "edit.php";
		$submenu_file = "EventManager/event-list.php";
	}

}
add_action('admin_menu', 'em_add_menu_pages');

function em_validation_js() {
	//if (strpos($_SERVER['REQUEST_URI'], get_em_registration_uri()) !== false) {
		$base_url = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? preg_replace('/http:/', 'https:', EM_BASE_URL) : EM_BASE_URL;
		echo "\n" . '<script src="' . $base_url . 'javascript/prototype.js" type="text/javascript"></script>';
		echo "\n" . '<script src="' . $base_url . 'javascript/formValidation.js" type="text/javascript"></script>' . "\n";
	//}
}
add_action('wp_head', 'em_validation_js');

function em_form_css() {
	//if (strpos($_SERVER['REQUEST_URI'], get_em_registration_uri()) !== false) {
		$base_url = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? preg_replace('/http:/', 'https:', EM_BASE_URL) : EM_BASE_URL;
		echo "\n" . '<link rel="stylesheet" href="' . $base_url . 'javascript/eventManager.css" type="text/css" media="screen" />';
	//}
}
add_action('wp_head', 'em_form_css');

function em_admin_head() {
	$page = $_REQUEST["page"];
	if (preg_match('/^EventManager/', $page)) {
		echo "\n<!-- START EventManager Includes -->";
		echo "\n" . '<script src="' . em_get_base_url() . 'javascript/prototype.js" type="text/javascript"></script>';
		echo "\n" . '<script src="' . em_get_base_url() . 'javascript/scriptaculous.js" type="text/javascript" ></script>';
		echo "\n" . '<script src="' . em_get_base_url() . 'javascript/calendar_stripped.js" type="text/javascript" ></script>';
		echo "\n" . '<script src="' . em_get_base_url() . 'javascript/calendar-en.js" type="text/javascript" ></script>';
		echo "\n" . '<script src="' . em_get_base_url() . 'javascript/calendar-setup.js" type="text/javascript" ></script>';
		echo "\n" . '<link rel="stylesheet" href="' . em_get_base_url() . 'javascript/eventManager.css" type="text/css" />';
		echo "\n" . '<link rel="stylesheet" href="' . em_get_base_url() . 'javascript/calendar-system.css" type="text/css" />';
		echo "<!-- END EventManager Includes -->\n";
	}
}
add_action('admin_head', 'em_admin_head');

function em_link_to_event($content) {
	if (preg_match_all("|em-link\[.+\]|", $content, $matches)) {
		foreach ($matches[0] as $match) {
			$parms_string = substr($match,strpos($match,"[") + 1);
			$parms_string = substr($parms_string, 0, strlen($parms_string)-1);
			$parms = explode(":", $parms_string);
			$name = $parms[1]?$parms[1]:'Register Now';
			$content = str_replace($match, '<a href="' . em_event_url($parms[0]) . '">'. $name . '</a>', $content);
		}
	}
	return $content;
}
add_filter('the_content', 'em_link_to_event');

function em_registration_page($content) {
	if (preg_match("/em-event-page\[\]/m", $content)) {
		return em_register_form();
	}
	return $content;
}
add_filter('the_content', 'em_registration_page');

function em_remove_title($content) {
	if (strpos($_SERVER['REQUEST_URI'], get_em_registration_uri()) !== false) {
		return "";
	}
	return $content;
}
add_filter('the_title', 'em_remove_title');

function em_register_submit() {
	global $wpdb,$reg_id,$em_registration_table;
	if ($_REQUEST['em_register_form'] == 1) {
		$event_id = $_POST['event_id'];
		$email_address = $_POST['email_address'];
//		if (0 < $wpdb->get_var("SELECT count(*) from $em_registration_table where event_id = '$event_id' and email_address = '$email_address'")) {
//			header("Location: " . $_SERVER['REQUEST_URI'] . '&em_error=alreadyregistered');
//		} else {
			$errors = em_insert_participant();
			if ($errors == false) {
				header("Location: " . $_SERVER['REQUEST_URI'] . '&confirm=' . $reg_id);
			} else {
				//header("Location: " . $_SERVER['REQUEST_URI'] . '&em_error=validation');
				require_once(ABSPATH . "wp-content/plugins/EventManager/register-form.php");
			}
//		}
		die;
	}
}

add_action('init', 'em_register_submit');